package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCouponDetailContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.CouponDetailBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletCouponDetailActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletCouponDetailPresenter implements WalletCouponDetailContract.Presenter {

    private WalletCouponDetailActivity context;
    private final WalletCouponDetailContract.View view;
    private Gson gson;

    public WalletCouponDetailPresenter(WalletCouponDetailActivity view,
                                       WalletCouponDetailActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getCouponDetail(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("sku_id", id);
        String params = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .getCouponDetail(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "goods.couponDetailFind", params)
                .subscribe(new ResponseObserver<BaseResponse<CouponDetailBean>>(context, true) {
                    @Override
                    public void onSuccess(BaseResponse<CouponDetailBean> response) {
                        view.getCouponDetailBack(true, null, response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getCouponDetailBack(false, msg, null);
                    }
                });
    }

}
